//
//  TrailerViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/3.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "TrailerViewController.h"
#import "TrailerCell.h"
#import "TrailerModel.h"
#import "AFNetworking.h"
#import <UIImageView+WebCache.h>
#import <MediaPlayer/MediaPlayer.h>

@interface TrailerViewController () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)UITableView *mainTableView;
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列
@property (nonatomic,retain)AFHTTPRequestOperation *operation2; // 操作2。
@property (nonatomic,retain) NSOperationQueue *queue2;  // 操作队列2
@property (nonatomic,retain)UIImageView *head;
@property (nonatomic,retain) NSDictionary *dic4;

@end

@implementation TrailerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.modelArr = [NSMutableArray array];
    [self createTableView];
    [self createHead];
    [self requestData];
    [self requestData2];
    
    // Do any additional setup after loading the view.
}

- (void)createTableView
{
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreen_width, kScreen_height - 64) style:UITableViewStylePlain];
    [_mainTableView registerClass:[TrailerCell class] forCellReuseIdentifier:@"trailercell"];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [self.view addSubview:_mainTableView];
    [_mainTableView release];
}


// 创建Head
- (void)createHead{
    self.head = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, kScreen_width, kScreen_width*320/640)];
    self.head.contentMode = UIViewContentModeScaleAspectFill;
    self.head.clipsToBounds = YES;
    self.mainTableView.tableHeaderView = self.head;
    [_head release];
}



// 请求轮播图数据
- (void)requestData2
{
    NSURL *url = [NSURL URLWithString:@"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation2 = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dic1 = dic[@"trailer"];
        self.dic4 = dic1;
        NSString *imageUrl = dic1[@"imageUrl"];
        [self.head sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
        self.head.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
        [self.head addGestureRecognizer:tapGest];
        [tapGest release];
        CGFloat kh = kScreen_width*320/640;
        CGFloat small = 0.2 *kh;
        UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(0, kh - small, kScreen_width, small)];
        smallView.backgroundColor = RGBColor(39, 41, 33);
        smallView.alpha = 0.95;
        [self.head addSubview:smallView];
        UILabel *title = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, kScreen_width - 30, small)];
        title.text = dic1[@"title"];
        title.font = [UIFont boldSystemFontOfSize:16];
        title.textColor = [UIColor whiteColor];
        title.textAlignment = NSTextAlignmentCenter;
        [smallView addSubview:title];
        UIImageView *play = [[UIImageView alloc] initWithFrame:CGRectMake(kScreen_width - small - 20, 0, small, small)];
        play.image = [UIImage imageNamed:@"iconfont-bofang(2)"];
        [smallView addSubview:play];
        [play release];
        [title release];
        [smallView release];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue2 = queue;
    [queue release];
}



// 头顶跳转
- (void)nextPage:(UIGestureRecognizer *)gest{
    NSString *str = self.dic4[@"mp4Url"];
    NSString *urlStr =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *movieURL = [NSURL URLWithString:urlStr];
    MPMoviePlayerViewController *myplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:myplayer];
    [myplayer release];

}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TrailerModel *model = [self.modelArr objectAtIndex:indexPath.row];
    
    TrailerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"trailercell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 95;
    
}


// AFN 数据请求
- (void)requestData
{
    NSURL *url = [NSURL URLWithString:@"http://api.m.mtime.cn/PageSubArea/TrailerList.api"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"trailers"];
        for (NSDictionary *dic in arr) {
            TrailerModel *model = [[TrailerModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
        }
        [self.mainTableView reloadData];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TrailerModel *model = [self.modelArr objectAtIndex:indexPath.row];
    NSString *str = model.url;
    NSString *urlStr =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *movieURL = [NSURL URLWithString:urlStr];
    MPMoviePlayerViewController *myplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:myplayer];
    [myplayer release];
}











-(void)dealloc
{
    [_dic4 release];
    [_head release];
    [_operation2 release];
    [_queue2 release];
    [_mainTableView release];
    [_operation release];
    [_queue release];
    [_modelArr release];
    [super dealloc];
}



@end
